﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Resources;
using System.Xml.Linq; 

namespace Engine.Scenario
{
    /// <summary>
    /// Script that represent Loading levels.
    /// </summary>
    public class Scenario
    {
        public Scenario()
        {
        }

        #region fields, events, constants
        /// <summary>
        /// Dictionary: key = levelId, value = LevelData
        /// </summary>
        public List<LevelData> levels = new List<LevelData>();
        /// <summary>
        /// Const value for last game level.
        /// </summary>
        public const int GameOverId = -1;
        /// <summary>
        /// Const value for first level in the game.
        /// </summary>
        public const int FirstLevelId = 1;        
        #endregion


        /// <summary>
        /// Get LevelData
        /// </summary>
        /// <param name="currentLevelId">Id of level</param>
        /// <returns>LevelData for provided levelId</returns>
        public LevelData GetLevelData(int levelID)
        {
            //watch out for last level!
            try
            {
                foreach (LevelData item in levels)
                {
                    if (item.ID == levelID)
                        return item;
                }

                throw new KeyNotFoundException();
            }
            catch (KeyNotFoundException exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Get next LevelData
        /// </summary>
        /// <param name="currentLevelId">Id of current level</param>
        /// <returns>LevelData of next level</returns>
        public LevelData GetNextLevelData(int currentLevelID)
        { 
            //watch out for last level!
            LevelData nextLevelData = null;
            try
            {
                nextLevelData = GetLevelData(currentLevelID);                
                return nextLevelData;
            }
            catch (KeyNotFoundException exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Add full description of level (including level and all items) to scenario
        /// </summary>
        /// <param name="levelData">Full descritption of level</param>
        public void AddLevelData(LevelData levelData)
        {
            levels.Add(levelData);
        }

        public static Scenario LoadScript(string fileName)
        {
            Scenario script = new Scenario();                        
            //get GameData
            StreamResourceInfo res = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
            XDocument doc = XDocument.Load(res.Stream);
            var query = from c in doc.Elements("levels").Elements("level") select c;
            foreach(var level in query)
            {

                LevelData levelData = new LevelData()
                        {
                            Description = level.Attribute("description").Value,
                            ID = Convert.ToInt32(level.Attribute("id").Value),
                            IDnextLevel = Convert.ToInt32(level.Attribute("nextLevelId").Value),
                            XamlFilePath = level.Attribute("xamlFilePath").Value,
                            PointsForLevel = Convert.ToInt32(level.Attribute("pointsForLevel").Value),
                            PenaltyForWrongClick = Convert.ToInt32(level.Attribute("penaltyForWrongClick").Value),
                            PointsForItem = Convert.ToInt32(level.Attribute("pointsForItem").Value),
                            Time = Convert.ToInt32(level.Attribute("time").Value),
                            PointsForSecond = Convert.ToInt32(level.Attribute("pointsForSecond").Value)
                        };
                //LevelData data = new LevelData();
                //data.LevelDescription = levelDesc;
                script.AddLevelData(levelData);
            }
            
            return script;
        }

        private static bool AttributeExists(XElement element, string name)
        {            
            foreach (XAttribute attr in element.Attributes())
            {
                if (attr.Name == name)
                    return true;
            }
            return false ;
        }
    }
}
